//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using Shp.Runtime.Sitka.ServiceAgent.Entities;
//using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;

//namespace Shp.Runtime.Sitka.ServiceAgent.EntityMappers
//{
//    public class EntityMapperT<T> : EntityMapper<T> where T : IEntity, new()
//    {
//        public override T ToType(Entity flexibleEntity)
//        {
//            var properties = typeof(T).GetProperties();

//            var notGeneric = properties.Where(p => typeof(Entity).GetProperty(p.Name) == null)
//                                       .Where(p => p.Name != "Fields" && p.Name != "IsCached" && p.Name != "Kind" )
//                                       .Select(p => new { Name = p.Name, Generic = false });

//            var generic = flexibleEntity.Properties.Keys.Except(notGeneric.Select(p => p.Name)).Select(p => new { Name = p, Generic = true });

//            var mappedProperties = notGeneric.Union(generic);

//            T t = new T();
//            t.Id = new Guid(flexibleEntity.Id);

//            if (t.GetType().GetProperty("Kind") != null)
//            { 
//                t.GetType().GetProperty("Kind").SetValue(t, flexibleEntity.Kind, null );
//            }

//            foreach (var property in mappedProperties)
//            {
//                if (property.Generic)
//                {
//                    Field field = new Field(property.Name, flexibleEntity.Properties[property.Name]);
//                    t.Fields.Add(field);
//                }
//                else
//                {
//                    var pty = properties.Where(p => p.Name == property.Name)
//                                        .SingleOrDefault();
//                    object value = Convert.ChangeType(flexibleEntity.Properties[property.Name], pty.PropertyType);
//                    pty.SetValue(t, value, null);

//                }
//            }

//            return t; ;
//        }

//        public override Entity FromType(T entity)
//        {
//            Entity f_entity = new Entity();
//            f_entity.Id = entity.Id.ToString();

//            if (entity.GetType().GetProperty("Kind") == null)
//            {
//                f_entity.Kind = entity.GetType().ToString();
//            }
//            else
//            {
//                f_entity.Kind = entity.GetType().GetProperty("Kind").GetValue(entity, null).ToString();
//            }
            
//            f_entity.Properties = new Dictionary<string, object>();

//            var properties = entity.GetType().GetProperties();
//            var notGeneric = properties.Where(p => p.Name != "Fields" && p.Name != "IsCached" && p.Name != "Id");

//            foreach (var p in notGeneric)
//            {
//                f_entity.Properties.Add(p.Name, p.GetValue(entity, null));
//            }

//            foreach (Field f in entity.Fields)
//            {
//                f_entity.Properties.Add(f.Name, f.Value);
//            }

//            return f_entity;
//        }
//    }
//}
